#include "SysCommons.h"
#include "Element.h"
#include "Table.h"
#include "Td.h"

#include "Utils.h"

Td::Td(const std::string &str)
:tabled(false) {
	setValue(str);
}

Td::Td(std::istream &fin)
:tabled(false) {
	char c;
	std::string val = "";
	while(!Utils::read(fin, c)) {
		if(c == '[') {
			std::cout << "New Table \n";
			attachChild(new Table(fin));
			tabled = true;
			continue;
		}
		else if(c == ',') {
			std::cout << "Row: " << val << "\n";
			break;
		}
		else if(c == ';') {
			std::cout << "Row: " << val << "\n";
			fin.unget();
			break;
		}

		val += c;
	}

	if(fin.eof()) {
		throw parsing_error;
	}

	setValue(val);
}

void Td::serialize(std::ostream &fout) {
	if(!tabled) {
		fout << "<td>" << getValue() << "</td> \n";
	} else {
		fout << "<td>\n";
		Element::serialize(fout);
		fout << "</td>\n";
	}
}